<?php

namespace common\models\trade;

use Yii;

/**
 * This is the model class for table "{{%trade_partner}}".
 *
 * @property int $id
 * @property string $label 商户名称
 * @property string $name 商户名称标识
 * @property string $app_id 商户应用ID
 * @property string $description 商户描述
 * @property string $sign 签名
 * @property int $account_id 收款账户ID
 * @property int $expire_time 签约到期时间
 * @property int $status 商户状态：-1取消，0未支付，1已支付
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class TradePartner extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%trade_partner}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['label', 'name', 'app_id', 'sign'], 'required'],
            [['account_id', 'expire_time', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['label'], 'string', 'max' => 100],
            [['name'], 'string', 'max' => 20],
            [['app_id'], 'string', 'max' => 16],
            [['description'], 'string', 'max' => 255],
            [['sign'], 'string', 'max' => 64],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'label' => '商户名称',
            'name' => '商户名称标识',
            'app_id' => '商户应用ID',
            'description' => '商户描述',
            'sign' => '签名',
            'account_id' => '收款账户ID',
            'expire_time' => '签约到期时间',
            'status' => '商户状态：-1取消，0未支付，1已支付',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    public function getAccount() {
        return $this->hasOne(TradeAccount::className(), ['id' => 'account_id']);
    }

    public function getAccountPaymodes() {
        return $this->hasMany(TradeAccountPaymode::className(), ['account_id' => 'account_id'])->active(TradeAccountPaymode::STATUS_ACTIVE);
    }

    public function getPaymodes() {
        return $this->hasMany(TradePaymode::className(), ['name' => 'paymode'])->via('accountPaymodes');
    }

}
